package com.example.lishop.repository

import com.example.lishop.entity.BUserEntity
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.transaction.annotation.Transactional

/**
 * Created on 2021/3/14 18:00
 * @author shuguo
 */
interface UserRepository : JpaRepository<BUserEntity, Int>{

    fun findByUserId(userId: String): BUserEntity?

    fun findByPhone(phone: String): BUserEntity?

    @Transactional(timeout = 10)
    @Modifying
    @Query("update BUserEntity bean set bean.sts = '1' where bean.userId = ?1 ")
    fun del(userId: String)

    //nativeQuery = true 表明sql语句，false表明hql语句，默认值是false
    @Transactional(timeout = 10)
    @Modifying
    @Query("update BUserEntity bean set " +
            "bean.name = CASE WHEN :#{#item.name} IS NULL THEN bean.name ELSE :#{#item.name} END " +
            ", bean.sex = CASE WHEN :#{#item.sex} IS NULL THEN bean.sex ELSE :#{#item.sex} END ," +
            "bean.phone = CASE WHEN :#{#item.phone} IS NULL THEN bean.phone ELSE :#{#item.phone} END ," +
            "bean.email = CASE WHEN :#{#item.email} IS NULL THEN bean.email ELSE :#{#item.email} END ," +
            "bean.pwd = CASE WHEN :#{#item.pwd} IS NULL THEN bean.avatar ELSE :#{#item.pwd} END ," +
            "bean.avatar = CASE WHEN :#{#item.avatar} IS NULL THEN bean.avatar ELSE :#{#item.avatar} END ," +
            "bean.type = CASE WHEN :#{#item.type} IS NULL THEN bean.type ELSE :#{#item.type} END ," +
            "bean.tag = CASE WHEN :#{#item.tag} IS NULL THEN bean.tag ELSE :#{#item.tag} END ," +
            "bean.status = CASE WHEN :#{#item.status} IS NULL THEN bean.status ELSE :#{#item.status} END ," +
            "bean.sts = CASE WHEN :#{#item.sts} IS NULL THEN bean.sts ELSE :#{#item.sts} END " +
            "where bean.userId = :#{#item.userId}"/*, nativeQuery = true*/)
    fun update(item:BUserEntity)
}